package com.inmaytide.website.mapper.system.provider;

import com.inmaytide.website.domain.entities.system.SysLog;
import com.inmaytide.website.utils.pagination.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Moss
 * @version 1.0.0
 * @since May 22, 2016
 */
public class SysLogMapperSQLProvider {

    public static final String TABLE_NAME = "sys_log";

    private void setConditions(SQL sql, Map<String, String> conditions) {
        if (StringUtils.isNotEmpty(conditions.get("operator"))) {
            sql.WHERE(String.format("b.name like '%%%s%%'", conditions.get("operator")));
        }
        if (StringUtils.isNotEmpty(conditions.get("content"))) {
            sql.WHERE(String.format("description like '%%%s%%'", conditions.get("content")));
        }
        if (StringUtils.isNotEmpty(conditions.get("optime_begin"))) {
            sql.WHERE(String.format("operating_time >= '%s'", conditions.get("optime_begin") + " 00:00:00"));
        }
        if (StringUtils.isNotEmpty(conditions.get("optime_end"))) {
            sql.WHERE(String.format("operating_time <= '%s'", conditions.get("optime_end") + " 23:59:59"));
        }
    }

    /**
     * @param parameters
     * @return
     */
    @SuppressWarnings("unchecked")
    public String findByMultipleConditionsAndPagination(Map<String, Object> parameters) {
        Pagination<SysLog> pagination = (Pagination<SysLog>) parameters.get("pagination");
        Map<String, String> conditions = (Map<String, String>) pagination.getConditions();
        return new StringBuilder(findListByMultipleConditionsProvider(conditions)).append(String.format(" LIMIT %d, %d ", pagination.getOffset(), pagination.getLimit())).toString();
    }

    @SuppressWarnings("unchecked")
    public String findCountByMultipleConditions(Map<String, Object> parameters) {
        Pagination<SysLog> pagination = (Pagination<SysLog>) parameters.get("pagination");
        Map<String, String> conditions = (Map<String, String>) pagination.getConditions();
        SQL sql = new SQL().SELECT("count(a.id)");
        sql.FROM(TABLE_NAME + " a");
        sql.LEFT_OUTER_JOIN("sys_user b on a.operator = b.id");
        setConditions(sql, conditions);
        return sql.toString();
    }


    @SuppressWarnings("unchecked")
    public String findListByMultipleConditions(Map<String, Object> parameters) {
        Map<String, String> conditions = ((Map<String, String>) parameters.get("conditions"));
        return findListByMultipleConditionsProvider(conditions);
    }


    private String findListByMultipleConditionsProvider(Map<String, String> conditions) {
        SQL sql = new SQL().SELECT("a.id, a.operator, b.name opname, a.operating_time, a.operator_ip, a.description, a.result");
        sql.FROM(TABLE_NAME + " a");
        sql.LEFT_OUTER_JOIN("sys_user b on a.operator = b.id");
        setConditions(sql, conditions);
        sql.ORDER_BY("operating_time DESC");
        return sql.toString();
    }

}
